﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Globalization;
using Public;

namespace Dal
{
    public class Category
    {
        public static DataTable MULTIPLE(string strCon)
        {
            DataTable dt = new DataTable();
            dt.Locale = CultureInfo.InvariantCulture;
            
            try
            {
                dt = SqlHelper.ExecuteDataTable(DataProtector.DecryptString(strCon), CommandType.StoredProcedure, "SP_Category_List_MULTIPLE");
            }
            catch
            {
                throw;
            }
            return dt;
        }
        

        public static DataTable MULTIPLE(string strCon, int strCatID) 
        {
            DataTable dt = new DataTable();
            dt.Locale = CultureInfo.InstalledUICulture;
            SqlParameter[] pars = { new SqlParameter("@CatId", strCatID) };
            try
            {
                dt = SqlHelper.ExecuteDataTable(DataProtector.DecryptString(strCon), CommandType.StoredProcedure, "SP_Category_MULTIPLE", pars);
            }
            catch (Exception)
            {
                throw;
            }
            return dt;
        }

        public static DataTable Single(string strCon, int pkid)
        {
            DataTable dt = new DataTable();
            dt.Locale = CultureInfo.InvariantCulture;
            SqlParameter[] pars = new SqlParameter[1];
            pars[0] = new SqlParameter("@PKID", SqlDbType.Int);
            pars[0].Value = pkid;

            try
            {
                dt = SqlHelper.ExecuteDataTable(DataProtector.DecryptString(strCon), CommandType.StoredProcedure, "SP_Category_SINGLE_PKID", pars);
            }
            catch
            {
                throw;
            }
            return dt;
        }

        public static DataTable Single(string strCon, string urlname)
        {
            DataTable dt = new DataTable();
            dt.Locale = CultureInfo.InvariantCulture;
            SqlParameter[] pars = new SqlParameter[1];
            pars[0] = new SqlParameter("@CatCode", SqlDbType.Int);
            pars[0].Value = urlname;

            try
            {
                dt = SqlHelper.ExecuteDataTable(DataProtector.DecryptString(strCon), CommandType.StoredProcedure, "SP_Category_SINGLE_URL", pars);
            }
            catch
            {
                throw;
            }
            return dt;
        }

        public static int Update(string strCon,
                string txbCatTitle,
                string txbUrlName,
                string txbImgURL,
                string txbDescription,
                int txbSortBy,
                int txbPKID)
        {
            int rtn = 0;
            SqlParameter[] pars = new SqlParameter[6];
            pars[0] = new SqlParameter("@CatName", SqlDbType.NVarChar, 50);
            pars[0].Value = txbCatTitle;
            pars[1] = new SqlParameter("@CatCode", SqlDbType.NVarChar);
            pars[1].Value = txbUrlName;
            pars[2] = new SqlParameter("@ImgURL", SqlDbType.NVarChar);
            pars[2].Value = txbImgURL;            
            pars[3] = new SqlParameter("@Description", SqlDbType.NVarChar);
            pars[3].Value = txbDescription;
            pars[4] = new SqlParameter("@SortBy", SqlDbType.TinyInt);
            pars[4].Value = txbSortBy;
            pars[5] = new SqlParameter("@PKID", SqlDbType.Int);
            pars[5].Value = txbPKID;

            try
            {
                rtn = SqlHelper.ExecuteNonQuery(DataProtector.DecryptString(strCon), CommandType.StoredProcedure, "SP_Category_UPDATE", pars);
            }
            catch
            {
                throw;
            }
            return rtn;
        }

        public static int Insert(string strCon,
                string txbCatTitle,
                string txbUrlName,
                string txbImgURL,
                string txbDescription,
                int txbSortBy,
                int txbCatRank,
                int txbParID)
        {
            int rtn = 0;
            SqlParameter[] pars = new SqlParameter[7];
            pars[0] = new SqlParameter("@CatName", SqlDbType.NVarChar, 50);
            pars[0].Value = txbCatTitle;
            pars[1] = new SqlParameter("@CatCode", SqlDbType.NVarChar);
            pars[1].Value = txbUrlName;
            pars[2] = new SqlParameter("@ImgURL", SqlDbType.NVarChar);
            pars[2].Value = txbImgURL;   
            pars[3] = new SqlParameter("@Description", SqlDbType.NVarChar);
            pars[3].Value = txbDescription;
            pars[4] = new SqlParameter("@SortBy", SqlDbType.TinyInt);
            pars[4].Value = txbSortBy;
            pars[5] = new SqlParameter("@CatRank", SqlDbType.Int);
            pars[5].Value = txbCatRank;
            pars[6] = new SqlParameter("@ParID", SqlDbType.Int);
            pars[6].Value = txbParID;

            try
            {
                rtn = SqlHelper.ExecuteNonQuery(DataProtector.DecryptString(strCon), CommandType.StoredProcedure, "SP_Category_INSERT", pars);
            }
            catch
            {
                throw;
            }
            return rtn;
        }

        public static int Insert(string strCon, string txbCatTitle,string txbUrlName,string txbImgURL,string txbDescription,int txbSortBy) 
        {
            int rtn = 0;
            SqlParameter[] pars = { 
                                    new SqlParameter("@CatName",txbCatTitle),
                                    new SqlParameter("@CatCode",txbUrlName),
                                    new SqlParameter("@ImgURL",txbImgURL),
                                    new SqlParameter("@Description",txbDescription),
                                    new SqlParameter("@SortBy",txbSortBy)
                                  };
            try
            {
                rtn = SqlHelper.ExecuteNonQuery(DataProtector.DecryptString(strCon), CommandType.StoredProcedure, "SP_Category_INSERT", pars);
            }
            catch (Exception)
            {
                throw;
            }
            return rtn;
        }

        public static int Delete(string strCon, int pkid)
        {
            int rtn = 0;
            SqlParameter[] pars = new SqlParameter[1];
            pars[0] = new SqlParameter("@PKID", SqlDbType.SmallInt);
            pars[0].Value = pkid;
            try
            {
                rtn = SqlHelper.ExecuteNonQuery(DataProtector.DecryptString(strCon), CommandType.StoredProcedure, "SP_Category_DELETE", pars);
            }
            catch
            {
                throw;
            }
            return rtn;
        }

    }
}
